﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
///
///HOW THIS ALL WORKS:
///Factions are individually defined objects that get entered into this manager, which is responsible for all general calls to faction functionality.
///When this manager is called to initialize, it automatically generates all five factions, assigns them starting cash, a color, and an Ai Type. 
///After, it finds a suitable (in theory anyway) spot to place their headquarters, and changes the Ownership Array to store a 3x3 block of faction ownership.
///The Building Array is also updated to include a 10 for Faction Headquarters. 
///Finally, the Unit Array is updated with a starting squad located within the Headquarters.
///
///After players are added to the player manager, there will be one more setup call to this manager to set up their attitudes.
///Every player is designed to have knoweledge of 2 factions and their locations, and those factions must have an "Acquainted" attitude towards said player.
///Other factions remain invisible under the Fog of War.
///
namespace WarPigs
{
    class FactionManager
    {
        Map map;
        public Faction plsFaction;
        public Faction colFaction;
        public Faction wesFaction;
        public Faction honFaction;
        public Faction marFaction;

        /// <summary>
        /// Setup Functions
        /// </summary>
        public void createFactions()
        {
            plsFaction = new Faction("People for the Liberation of Someralia", Color.OrangeRed, 1000, 1);
            colFaction = new Faction("The Columod", Color.Brown, 1000, 2);
            wesFaction = new Faction("Western Alliance", Color.Blue, 1000, 3);
            honFaction = new Faction("Honor Guard" , Color.OliveDrab , 1000 , 4);
            marFaction = new Faction("Marxist Extremists" , Color.IndianRed , 1000 , 5);
        }

        public void placeFactions()
        {
            //Find a spot for the faction HQ and build it
            Build("Headquarters", "plsFaction", GetHQCoordinates());
            Build("Headquarters", "colFaction", GetHQCoordinates());
            Build("Headquarters", "wesFaction", GetHQCoordinates());
            Build("Headquarters", "honFaction", GetHQCoordinates());
            Build("Headquarters", "marFaction", GetHQCoordinates());

            //Place starting units
        }

        public Vector2 GetHQCoordinates()
        {
            Vector2 coordinates = new Vector2();
            Random random = new Random();
            int ranX = random.Next(10, map.terrainarray.GetLength(0) - 10);
            int ranY = random.Next(10, map.terrainarray.GetLength(1) - 10);

            if (map.terrainarray != null)
            {
                while (0 < 1)
                {
                    if (map.ownershiparray[ranX, ranY] == 0 && map.ownershiparray[ranX + 1, ranY] == 0 && map.ownershiparray[ranX - 1, ranY] == 0 && map.ownershiparray[ranX, ranY + 1] == 0 &&
                    map.ownershiparray[ranX, ranY - 1] == 0 && map.ownershiparray[ranX + 1, ranY + 1] == 0 && map.ownershiparray[ranX + 1, ranY - 1] == 0)
                    {
                        if (map.terrainarray[ranX, ranY] == 1 || map.terrainarray[ranX, ranY] == 5 || map.terrainarray[ranX, ranY] == 6 || map.terrainarray[ranX, ranY] == 7 && map.ownershiparray[ranX, ranY] == 0)
                        {
                            if (ranY % 2 != 0)
                            {
                                coordinates = new Vector2(ranX, ranY);
                                break;
                            }
                            else
                            {
                                ranX = random.Next(10, map.terrainarray.GetLength(0) - 10);
                                ranY = random.Next(10, map.terrainarray.GetLength(1) - 10);
                                continue;
                            }
                        }
                        else
                        {
                            ranX = random.Next(10, map.terrainarray.GetLength(0) - 10);
                            ranY = random.Next(10, map.terrainarray.GetLength(1) - 10);
                            continue;
                        }
                    }
                    else
                    {
                        ranX = random.Next(10, map.terrainarray.GetLength(0) - 10);
                        ranY = random.Next(10, map.terrainarray.GetLength(1) - 10);
                        continue;
                    }
                }
            }

            return coordinates;
        }

        /// <summary>
        /// Constructs buildings by adding them to the Building Array.
        /// 
        /// BUILDINGS LIST
        /// PMC BUILDINGS
        /// 
        /// 0 - Headquarters
        /// 
        /// FACTION BUILDINGS
        /// 12 - Headquarters
        /// </summary>
        /// <param name="Building"></param>
        /// <param name="faction"></param>
        /// <param name="Coordinate"></param>
        public void Build(string Building, string faction, Vector2 Coordinate)
        {
            if (Coordinate.X > 0)
            {
                switch (Building)
                {
                    case "Headquarters":
                        //Sets the building
                        map.buildingarray[(int)Coordinate.X, (int)Coordinate.Y] = 12;
                        //Lays the control zone
                        UpdateOwnership((int)Coordinate.X, (int)Coordinate.Y, faction);
                        break;
                }
            }
        }

        public void UpdateOwnership(int x, int y, string faction)
        {
            switch(faction)
            {
                case "plsFaction":
                    map.ownershiparray[x, y] = 1;
                    map.ownershiparray[x + 1, y] = 1;
                    map.ownershiparray[x - 1, y] = 1;
                    map.ownershiparray[x, y + 1] = 1;
                    map.ownershiparray[x, y - 1] = 1;
                    map.ownershiparray[x + 1, y + 1] = 1;
                    map.ownershiparray[x + 1, y - 1] = 1;
                    break;
                case "colFaction":
                    map.ownershiparray[x, y] = 2;
                    map.ownershiparray[x + 1, y] = 2;
                    map.ownershiparray[x - 1, y] = 2;
                    map.ownershiparray[x, y + 1] = 2;
                    map.ownershiparray[x, y - 1] = 2;
                    map.ownershiparray[x + 1, y + 1] = 2;
                    map.ownershiparray[x + 1, y - 1] = 2;
                    break;
                case "wesFaction":
                    map.ownershiparray[x, y] = 3;
                    map.ownershiparray[x + 1, y] = 3;
                    map.ownershiparray[x - 1, y] = 3;
                    map.ownershiparray[x, y + 1] = 3;
                    map.ownershiparray[x, y - 1] = 3;
                    map.ownershiparray[x + 1, y + 1] = 3;
                    map.ownershiparray[x + 1, y - 1] = 3;
                    break;
                case "honFaction":
                    map.ownershiparray[x, y] = 4;
                    map.ownershiparray[x + 1, y] = 4;
                    map.ownershiparray[x - 1, y] = 4;
                    map.ownershiparray[x, y + 1] = 4;
                    map.ownershiparray[x, y - 1] = 4;
                    map.ownershiparray[x + 1, y + 1] = 4;
                    map.ownershiparray[x + 1, y - 1] = 4;
                    break;
                case "marFaction":
                    map.ownershiparray[x, y] = 5;
                    map.ownershiparray[x + 1, y] = 5;
                    map.ownershiparray[x - 1, y] = 5;
                    map.ownershiparray[x, y + 1] = 5;
                    map.ownershiparray[x, y - 1] = 5;
                    map.ownershiparray[x + 1, y + 1] = 5;
                    map.ownershiparray[x + 1, y - 1] = 5;
                    break;
            }
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="Map"></param>
        public FactionManager(Map Map)
        {
            map = Map;
        }
    }
}
